package com.api;

import java.nio.charset.Charset;
import java.util.List;

import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.StringEntity;

import com.bean.BaseResult;
import com.bean.WeixinUser;
import com.utils.JsonUtil;
import com.utils.LocalHttpClient;

public class UserAPI extends BaseAPI {

    /**
     * 获取用户基本信息
     *
     * @param access_token
     * @param openid
     * @return
     * https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
     */
    public static WeixinUser userInfo(String access_token, String openid) {
        HttpUriRequest httpUriRequest = RequestBuilder.post()
                .setUri(BASE_URI + "/sns/userinfo")
                .addParameter("access_token", access_token)
                .addParameter("openid", openid)
                .addParameter("lang", "zh_CN")
                .build();
        return LocalHttpClient.executeJsonResult(httpUriRequest, WeixinUser.class);
    }

    /**
     * 设置备注名
     *
     * @param access_token
     * @param openid
     * @param remark
     * @return
     */
    public static BaseResult userInfoUpdateremark(String access_token, String openid, String remark) {
        String postJson = String.format("{\"openid\":\"%1$s\",\"remark\":\"%2$s\"}", openid, remark);
        HttpUriRequest httpUriRequest = RequestBuilder.post()
                .setHeader(jsonHeader)
                .setUri(BASE_URI + "/cgi-bin/user/info/updateremark")
                .addParameter("access_token", access_token)
                .setEntity(new StringEntity(postJson, Charset.forName("utf-8")))
                .build();
        return LocalHttpClient.executeJsonResult(httpUriRequest, BaseResult.class);
    }


    /**
     * 修改分组名
     *
     * @param access_token
     * @param id           分组ID
     * @param name         分组名
     * @return
     */
    public static BaseResult groupsUpdate(String access_token, String id, String name) {
        String groupJson = "{\"group\":{\"id\":" + id + ",\"name\":\"" + name + "\"}}";
        HttpUriRequest httpUriRequest = RequestBuilder.post()
                .setHeader(jsonHeader)
                .setUri(BASE_URI + "/cgi-bin/groups/update")
                .addParameter("access_token", access_token)
                .setEntity(new StringEntity(groupJson, Charset.forName("utf-8")))
                .build();
        return LocalHttpClient.executeJsonResult(httpUriRequest, BaseResult.class);
    }

    /**
     * 移动用户分组
     *
     * @param access_token
     * @param openid
     * @param to_groupid
     * @return
     */
    public static BaseResult groupsMembersUpdate(String access_token, String openid, String to_groupid) {
        String groupJson = "{\"openid\":\"" + openid + "\",\"to_groupid\":" + to_groupid + "}";
        HttpUriRequest httpUriRequest = RequestBuilder.post()
                .setHeader(jsonHeader)
                .setUri(BASE_URI + "/cgi-bin/groups/members/update")
                .addParameter("access_token", access_token)
                .setEntity(new StringEntity(groupJson, Charset.forName("utf-8")))
                .build();
        return LocalHttpClient.executeJsonResult(httpUriRequest, BaseResult.class);
    }


    /**
     * 批量移动用户分组
     *
     * @param access_token
     * @param openid_list
     * @param to_groupid
     * @return
     */
    public static BaseResult groupsMembersBatchUpdate(String access_token, List<String> openid_list, String to_groupid) {
        String openidListStr = JsonUtil.toJSONString(openid_list);
        String groupJson = "{\"openid_list\":" + openidListStr + ",\"to_groupid\":" + to_groupid + "}";
        HttpUriRequest httpUriRequest = RequestBuilder.post()
                .setHeader(jsonHeader)
                .setUri(BASE_URI + "/cgi-bin/groups/members/batchupdate")
                .addParameter("access_token", access_token)
                .setEntity(new StringEntity(groupJson, Charset.forName("utf-8")))
                .build();
        return LocalHttpClient.executeJsonResult(httpUriRequest, BaseResult.class);
    }

    /**
     * 删除分组
     *
     * @param access_token
     * @param id
     * @return
     */
    public static BaseResult groupsDelete(String access_token, String id) {
        String groupJson = String.format("{\"group\":{\"id\":%1$s}}", id);
        HttpUriRequest httpUriRequest = RequestBuilder.post()
                .setHeader(jsonHeader)
                .setUri(BASE_URI + "/cgi-bin/groups/delete")
                .addParameter("access_token", access_token)
                .setEntity(new StringEntity(groupJson, Charset.forName("utf-8")))
                .build();
        return LocalHttpClient.executeJsonResult(httpUriRequest, BaseResult.class);
    }

}
